﻿Imports System.Windows.Forms

Public Class CodeList
    Private Sub CodeList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CodeList_Handle = Me.Handle
        ListBox1.Left = 0
        ListBox1.Width = 300
        ListBox1.Height = (292 + ListBox1.ItemHeight)
        RichTextBox1.Left = 150
        RichTextBox1.Width = 300
        RichTextBox1.Height = 292
        For i = 1 To UBound(CodeArr)
            ListBox1.Items.Add(CodeArr(i, 2))
        Next

        'Debug.Print(My.Computer.Screen.Bounds.Width.ToString())
        'Debug.Print(My.Computer.Screen.Bounds.Height.ToString())


    End Sub

    Private Sub CodeList_LostFocus(sender As Object, e As EventArgs) Handles Me.LostFocus
        '/失去焦点就隐藏
        ' Me.Hide()

    End Sub

    Protected Overrides Function ProcessDialogKey(keyData As Keys) As Boolean
        If keyData = Keys.Up OrElse keyData = Keys.Down OrElse keyData = Keys.Left OrElse keyData = Keys.Right Then
            Return False
        Else
            Return MyBase.ProcessDialogKey(keyData)
        End If
    End Function


    Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click
        Dim SelInfo As SelLineColInfo = VBGetSelection() '获取光标所在位置
        Dim KeyStr As String = RTrim(_VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, 1)) '获取当前行数据

        Dim arr As Object = Split(KeyStr, " ") '利用空格写入数组
        Dim tempstr As String
        If UBound(arr) = 0 Then
            For i = 0 To UBound(arr)
                tempstr = ""
            Next
        Else
            For i = 0 To UBound(arr) - 1
                tempstr = tempstr & " " & arr(i)
            Next
        End If
        _VBE.ActiveCodePane.CodeModule.ReplaceLine(SelInfo.SLine, Trim(tempstr & Replace(Replace(Me.RichTextBox1.Text, "{", ""), "}", ""))) '利用替换行的方法，写入代码
        SetWindowPos(CodeList_Handle, 0, 0, 0, 0, 0, SWP_HIDEWINDOW) '隐藏窗口
        SetFocus(ActiveCodePane_Handle) '获取焦点
        '//开始设置选中位置，已{}作为特征分隔符
        'Dim SelCode As String = _VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, SelInfo.ELine)
        If InStr(Me.RichTextBox1.Text, "{") > 0 And InStr(Me.RichTextBox1.Text, "}") > 0 Then
            _VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(tempstr & Me.RichTextBox1.Text), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(tempstr & Me.RichTextBox1.Text))
            '_VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(SelCode), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(SelCode))
        Else
            _VBE.ActiveCodePane.SetSelection(SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text), SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text))
        End If
    End Sub

    Private Sub ListBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseMove
        Try
            ListBox1.SelectedIndex = ListBox1.IndexFromPoint(e.Location)
        Catch

        End Try


        ' Try
        '  Dim pos As Integer
        '  pos = e.X / Screen.PrimaryScreen.Bounds.Width + e.Y / Screen.PrimaryScreen.Bounds.Height * 65536
        ' If pos <= 25887 And pos >= 0 Then
        '     ListBox1.SelectedIndex = ListBox1.TopIndex + pos \ 1500
        '     RichTextBox1.Text = DCodeArr.Item(ListBox1.SelectedItem.ToString)
        '  End If
        'Catch

        'End Try
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Try
            If ListBox1.SelectedItem Is Nothing Then Exit Sub
            RichTextBox1.Text = DCodeArr.Item(ListBox1.SelectedItem.ToString)
        Catch

        End Try
    End Sub



    Private Sub ListBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ListBox1.KeyPress
        Dim SelInfo As SelLineColInfo = VBGetSelection() '获取光标所在位置
        Dim KeyStr As String = RTrim(_VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, 1)) '获取当前行数据
        Dim arr As Object = Split(KeyStr, " ") '利用空格写入数组
        Dim tempstr As String
        If UBound(arr) = 0 Then
            For i = 0 To UBound(arr)
                tempstr = ""
            Next
        Else
            For i = 0 To UBound(arr) - 1
                tempstr = tempstr & " " & arr(i)
            Next
        End If

        If e.KeyChar = Chr(27) Or e.KeyChar = Chr(8) Then 'ESC和删除键，直接隐藏窗体
            'Me.Hide()
            ESC_TN = True
            SetFocus(ActiveHandle)
            '//开始设置选中位置，已{}作为特征分隔符
            'Dim SelCode As String = _VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, SelInfo.ELine)
            If InStr(Me.RichTextBox1.Text, "{") > 0 And InStr(Me.RichTextBox1.Text, "}") > 0 Then
                _VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(tempstr & Me.RichTextBox1.Text), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(tempstr & Me.RichTextBox1.Text))
                '_VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(SelCode), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(SelCode))
            Else
                _VBE.ActiveCodePane.SetSelection(SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text), SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text))
            End If


        ElseIf e.KeyChar = Chr(13) Then '回车键，直接写代码到代码编辑区


            _VBE.ActiveCodePane.CodeModule.ReplaceLine(SelInfo.SLine, Trim(tempstr & Replace(Replace(Me.RichTextBox1.Text, "{", ""), "}", ""))) '利用替换行的方法，写入代码
            SetWindowPos(CodeList_Handle, 0, 0, 0, 0, 0, SWP_HIDEWINDOW) '隐藏窗口
            SetFocus(ActiveCodePane_Handle) '获取焦点
            '//开始设置选中位置，已{}作为特征分隔符
            'Dim SelCode As String = _VBE.ActiveCodePane.CodeModule.Lines(SelInfo.SLine, SelInfo.ELine)
            If InStr(Me.RichTextBox1.Text, "{") > 0 And InStr(Me.RichTextBox1.Text, "}") > 0 Then
                _VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(tempstr & Me.RichTextBox1.Text), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(tempstr & Me.RichTextBox1.Text))
                '_VBE.ActiveCodePane.SetSelection(SelInfo.SLine + GetSLine(Me.RichTextBox1.Text), GetSCol(SelCode), SelInfo.ELine + GetELine(Me.RichTextBox1.Text), GetECol(SelCode))
            Else
                _VBE.ActiveCodePane.SetSelection(SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text), SelInfo.SLine, Len(tempstr & Me.RichTextBox1.Text))
            End If
        ElseIf e.KeyChar = Chr(40) Then
            MsgBox(2)


        End If
    End Sub



    Function GetSCol(a) As Int16
        Dim trr As Object = Split(a, vbLf)
        For i = 0 To UBound(trr)
            Dim s As Int16 = InStr(trr(i), "{")
            If s > 0 Then
                GetSCol = System.Text.Encoding.Default.GetByteCount(Mid(trr(i), 1, s))
                Exit Function
            End If
        Next
        Return GetSCol
    End Function

    Function GetECol(a) As Int16
        Dim trr As Object = Split(a, vbLf)
        For i = 0 To UBound(trr)

            Dim s As Int16 = InStr(trr(i), "}")
            If s > 0 Then
                GetECol = System.Text.Encoding.Default.GetByteCount(Mid(trr(i), 1, s)) - 1
                Exit Function
            End If
            'For j = 1 To System.Text.Encoding.Default.GetByteCount(trr(i))
            'If Mid(trr(i), j, 1) = "}" Then
            'GetECol = j
            'Exit Function
            'End If
            'Next
        Next
        Return GetECol
    End Function

    Function GetSLine(a) As Int16
        Dim trr As Object = Split(a, vbLf)
        For i = 0 To UBound(trr)
            If InStr(trr(i), "{") Then
                GetSLine = i
                Exit Function
            End If
        Next
        Return GetSLine
    End Function

    Function GetELine(a) As Int16
        Dim trr As Object = Split(a, vbLf)
        For i = 0 To UBound(trr)
            If InStr(trr(i), "}") Then
                GetELine = i
                Exit Function
            End If
        Next
        Return GetELine
    End Function

    Private Sub CodeList_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        MsgBox(e.KeyCode.ToString)
    End Sub

    Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged

    End Sub
End Class